<?php
		$version = Zend_Registry::get('version');
		echo $this->headScript()->setFile($this->baseUrl().'/js/jquery/jquery.scrollTo.js');		
		$this->articleForms->setEditor($this->serverUrl().$this->baseUrl());
		$this->articleForms->getEditor();	
?>
<style type="text/css">
	.CodeMirror, .codemirror-ui-clearfix{width:602px;}
</style>
<script type="text/javascript">
$(document).ready(function() {
	floatingbar('#floatingbar',"#button_top_bar",null);
	/**********Scrolling Tools Start****************/
	$.easing.elasout = function(x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	};
	
	$('#toc a').click(function(){//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
		$.scrollTo( this.hash, 1500, { easing:'swing' });
		$(this.hash).find('span.message').text( this.title );
		return false;
	});
	
	$('span.top-go a').click(function(){//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
		$.scrollTo( this.hash, 1500, { easing:'swing' });		
		return false;
	});
	
	/**********Scrolling Tools END****************/
	
	$('a.reset_btn').click(function() {
		resetFields();
		removeError();
	});
	
	function resetFields()
	{
		$( "form" )[ 0 ].reset();
		$('#menu_list').val('');
		if(editor){ editor.mirror.setValue(''); }
		$('#last_id').val('');
		$('#article_name_field').val('');
		$('span.create_menu_btn').css('visibility','hidden');	
		if(tinyMCE.get('article_content'))
		{
			tinyMCE.get('article_content').setContent('');
		}
	}
	
	function removeError()
	{
		$('#actionMessage').html('');
					
		$.each($('form').serializeArray(), function(i, field) {				
				$('#'+field.name).removeClass('ui-state-error');
				$('#'+field.name+'_err').html('');									
		});				
	}
	
	function openLoaderDialog()
	{
		$('#dialog_container').dialog({
					autoOpen: true,
					modal: true,
					show: 'fade',
					resizable: 'false',				
					open: function(event, ui)
						 {
						 	$("#progressbar").progressbar({
								value:100
				
							});							
						  }
				});
	}
	
	function closeLoaderDialog()
	{
		$('#dialog_container').dialog('option','hide','explode').dialog('close');
	}
	
	function addError(key,errMsg)
	{
		var err_id = key+'_err';
		var field_name = key;
		$('#'+field_name).addClass('ui-state-error');
		$('#'+err_id).append(errMsg);
	}
	
	function getFormData()
	{
		var fieldObj = {};
		$.each($('#formArticle').serializeArray(), function(i, field) {			
			if(tinyMCE.get(field.name))
			{
				fieldObj[field.name] = tinyMCE.get(field.name).getContent();
			}
			else
			{
				fieldObj[field.name] = (editor && field.name == 'article_content') ? editor.mirror.getValue() : field.value;
			}						
		});
		return fieldObj;
	}
	
	$('a.add_article_btn').click(function() {	
		
		var formData = getFormData();	
		var menu_list	=	$("select[name='menu_list']").val();
		
		if(menu_list != null && menu_list != '')
		{
			formData.menu_list = menu_list;	
			confirmFunction(formData);
		}
		else
		{
			addFunction(formData);
		}
				
	});
	
	
function addFunction(formData)
{
	$.ajax({
				url: "<?php echo $this->url( array('module' => 'Articles','controller' => 'Backend', 'action'     => 'add' ), 'adminrout',    true) ?>",
					type: 'POST',
					data: formData,
					beforeSend: function(){						
							removeError();
							openLoaderDialog();				
						 },
					success: function(response){					
						var json_arr = eval("("+response+")");
						closeLoaderDialog();
						if(json_arr.status == 'ok')
						{
							$('#actionMessage').html(succMsgDesign(json_arr.msg));
							resetFields();
							$('#last_id').val(json_arr.last_id);	
							$('#article_name_field').val(formData.article_name);
							$('span.create_menu_btn').css('visibility','visible');
							$('span.create_menu_btn').children("a:nth-child(1)").attr('href','<?php echo $this->url( array("module" => "Menu","controller" => "Backend", "action"     => "add","article_id" => "" ), "adminrout",    true) ?>'+json_arr.last_id);				
						}
						else if(json_arr.status == 'errV')
						{
							$('#actionMessage').html(errMsgDesign("<?php echo $this->translator->translator('common_err'); ?>"));
													
							for(var i= 0; i < json_arr.msg.length; i++)
							{
								focusFirst(json_arr.msg[i].key, json_arr.msg[i].value, i);
								addError(json_arr.msg[i].key,json_arr.msg[i].value);
							}
						}
						else
						{
							$('#actionMessage').html(errMsgDesign(json_arr.msg));
						}
					},
					error: function(xhr, status, error){
						closeLoaderDialog();
						var msg = "Error! " + xhr.status + " " + error;					
						$('#actionMessage').html(errMsgDesign(msg));
					}
			});
}

function confirmFunction(formData)
{
	$("#dialog-confirm").html("<?php echo addslashes($this->translator->translator('article_list_save_confirm')); ?>").dialog({
					resizable: false,
					width:400,
					height:'auto',
					modal: true,
					show: 'fade',
					hide: 'explode',
					buttons: {
							'<?php echo html_entity_decode($this->translator->translator("common_ok"), ENT_QUOTES, "UTF-8"); ?>': function() {
							$(this).dialog('close');
							addFunction(formData);														
						},
					'<?php echo html_entity_decode($this->translator->translator("common_cancel"), ENT_QUOTES, "UTF-8"); ?>': function() {
							$(this).dialog('close');							
						}
					}
				});	
}
	
	
});


 
</script>
<div id="container">
<?php if (!$this->allow()) : ?>
	<div class="grid_12 t_a_bottom">
		<?php echo $this->translator->translator('Member_Access_deny_desc'); ?>
	</div>
<?php else : ?>

<div class="title-container">
	<div class="article-new"><h1><?php echo $this->translator->translator('article_add_page_name'); ?></h1></div>
    
    <div class="button-container">
    	<?php if($this->popup != 'popup'){ ?><span class="button ui-widget-content-1 create_menu_btn" style="visibility:hidden;"><a href=""><span class="icon link"><?php echo $this->translator->translator('article_page_create_menu_link'); ?></span></a></span><?php } ?>
    	<?php if($this->popup != 'popup'){ ?>
        <span class="button ui-widget-content-1"><a href="<?php echo $this->url( array('module' => 'Articles','controller' => 'Backend', 'action'     => 'list' ), 'adminrout',    true) ?>"><span class="icon back"><?php echo $this->translator->translator('common_back'); ?></span></a></span>
        <?php } ?>
        <span class="button ui-widget-content-1"><a href="javascript:  void(0);" class="reset_btn"><span class="icon reset"><?php echo $this->translator->translator('common_reset'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript: void(0);" class="add_article_btn"><span class="icon save"><?php echo $this->translator->translator('common_save'); ?></span></a></span>
    </div>
</div>

<div class="content-container">
	<div id="sidebar-inner">
    	<?php echo $this->partial('backend/article_menu.phtml');  ?>
        <?php if($this->popup != 'popup'){ ?>
        <div>&nbsp;</div>
        <div id="nav-tree">
    		<div class="nav-header"><div class="nav-title"><?php echo $this->translator->translator('menu_list_selection','', "Menu"); ?></div></div>
    		<div id="categoryTree" class="nav-body treebody" >
           		<select name="menu_list" id="menu_list" multiple="multiple" size="35">
                    <option value=""><?php echo $this->translator->translator('articles_link_no_menu'); ?></option>
                    <?php echo Menu_View_Helper_SubMenu::getAllMenuList(); ?>
                </select>
            </div>
       	</div>
        <?php } ?>
    </div>
    
    <div id="topGo">
    	<div id="content-inner">
    		<form name="formArticle" id="formArticle" method="post">
            	<div id="actionMessage"></div>
                <div id="button_top_bar"></div>
                <div id="generalInfo">
                    <fieldset class="fieldset">
                        <legend class="legend"><?php echo $this->translator->translator('article_menu_general_info'); ?></legend>
                        <span class="top-go right-float"><a href="#topGo"><img src="application/modules/Administrator/layouts/scripts/images/tools/top.png" border="0" alt="<?php echo $this->translator->translator('common_go_top_title'); ?>" title="<?php echo $this->translator->translator('common_go_top_title'); ?>" /></a></span>
                        <input type="hidden" id="last_id" name="last_id" value="" /><input type="hidden" id="article_name_field" name="article_name_field" value="" />
                        <div class="form">
                            <div class="form-row">
                                <div class="label article-label">
                                    <?php echo $this->articleForms->article_name->getLabel(); if($this->articleForms->article_name->isRequired()){ ?><span class="required">*</span><?php } ?>
                                </div>
                    
                                <div class="field article-field">
                                    <?php  echo $this->articleForms->article_name;  ?>
                                    <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->escape($this->articleForms->article_name->getAttrib('title')); ?>"><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                                    <div class="input-errors" id="article_name_err"></div>
                                </div>
                            </div>
                            
                            <div class="form-row">
                                <div class="label article-label">
                                    <?php echo $this->articleForms->article_title->getLabel(); if($this->articleForms->article_title->isRequired()){ ?><span class="required">*</span><?php } ?>
                                </div>
                    
                                <div class="field article-field">
                                    <?php  echo $this->articleForms->article_title;  ?>
                                    <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->escape($this->articleForms->article_title->getAttrib('title')); ?>"><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                                    <div class="input-errors" id="article_title_err"></div>
                                </div>
                            </div>
                            
                            <div class="form-row">
                                <div class="label article-label">
                                    <?php echo $this->articleForms->article_content->getLabel(); if($this->articleForms->article_content->isRequired()){  ?><span class="required">*</span><?php } ?>
                                </div>
                    
                                <div class="field article-field">
                                    <div class="left-float"><?php echo $this->articleForms->article_content; ?></div>
                                    <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->escape($this->articleForms->article_content->getAttrib('title')); ?>"><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                                    <span id="loaderLink"><a href="javascript:void(0);" class="loaderLink_class" rel="wysiwyg,article_content"><img src="application/modules/Administrator/layouts/scripts/images/common/editors.gif" border="0" title="<?php echo $this->translator->translator('common_editor_open'); ?>" alt="<?php echo $this->translator->translator('common_editor_open'); ?>" ></a></span>
                                    <div class="input-errors" id="article_content_err"></div>
                                </div>	
                            </div>
                        </div>
                     </fieldset>   
                </div>
                
                <div id="metadata">
                	<fieldset class="fieldset">
                        <legend class="legend"><?php echo $this->translator->translator('article_menu_meta_info'); ?></legend>
                        <span class="top-go right-float"><a href="#topGo"><img src="application/modules/Administrator/layouts/scripts/images/tools/top.png" border="0" alt="<?php echo $this->translator->translator('common_go_top_title'); ?>" title="<?php echo $this->translator->translator('common_go_top_title'); ?>" /></a></span>
                        <div class="form">
                            <div class="form-row">
                                <div class="label article-label">
                                    <?php echo $this->articleForms->article_meta_title->getLabel(); if($this->articleForms->article_meta_title->isRequired()){ ?><span class="required">*</span><?php } ?>
                                </div>
                    
                                <div class="field article-field">
                                    <?php echo $this->articleForms->article_meta_title; ?>
                                    <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->escape($this->articleForms->article_meta_title->getAttrib('title')); ?>"><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                                    <div class="input-errors" id="article_meta_title_err"></div>
                                </div>
                            </div>
          
                            <div class="form-row">
                                <div class="label article-label">
                                    <?php echo $this->articleForms->article_meta_key->getLabel();  if($this->articleForms->article_meta_key->isRequired()){ ?><span class="required">*</span><?php } ?>
                                </div>
                    
                                <div class="field article-field">
                                    <?php echo $this->articleForms->article_meta_key; ?>
                                    <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->escape($this->articleForms->article_meta_key->getAttrib('title')); ?>"><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                                    <div class="input-errors" id="article_meta_key_err"></div>
                                </div>
                            </div>
                            
                            <div class="form-row">
                                <div class="label article-label">
                                    <?php echo $this->articleForms->article_meta_desc->getLabel();  if($this->articleForms->article_meta_desc->isRequired()){ ?><span class="required">*</span><?php } ?>
                                </div>
                    
                                <div class="field article-field">
                                    <?php echo $this->articleForms->article_meta_desc; ?>
                                    <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->escape($this->articleForms->article_meta_desc->getAttrib('title')); ?>"><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                                    <div class="input-errors" id="article_meta_desc_err"></div>
                                </div>
                            </div>
                            
                   		</div>
					</fieldset>
				</div>
             </form>
		</div>
 	</div>
    
    <!--<div class="button-container">
    	<?php //if($this->popup != 'popup'){ ?>
        <span class="button ui-widget-content-1"><a href="<?php //echo $this->url( array('module' => 'Articles','controller' => 'Backend', 'action'     => 'list' ), 'adminrout',    true) ?>"><span class="icon back"><?php //echo $this->translator->translator('common_back'); ?></span></a></span>
        <?php //} ?>
        <span class="button ui-widget-content-1"><a href="javascript:  void(0);" class="reset_btn"><span class="icon reset"><?php //echo $this->translator->translator('common_reset'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript: void(0);" class="add_article_btn"><span class="icon save"><?php //echo $this->translator->translator('common_save'); ?></span></a></span>
    </div>	-->
</div>

<div id="dialog_container" style="display:none;" title="<?php echo $this->translator->translator('common_processing_language'); ?>">
    <div id="progressbar" ></div>	
</div>
<div id="dialog-confirm" title="<?php echo $this->translator->translator('common_confirmation',$this->translator->translator('common_save')); ?>"></div>

<div id="floatingbar">
	<h1><?php echo $this->translator->translator('article_add_page_name'); ?></h1>
    
    <div class="button-container">
    	<?php if($this->popup != 'popup'){ ?>
        <span class="button ui-widget-content-1 create_menu_btn" style="visibility:hidden;"><a href=""><span class="icon link"><?php echo $this->translator->translator('article_page_create_menu_link'); ?></span></a></span>
        <?php } ?>
        <?php if($this->popup != 'popup'){ ?>
        <span class="button ui-widget-content-1"><a href="<?php echo $this->url( array('module' => 'Articles','controller' => 'Backend', 'action'     => 'list' ), 'adminrout',    true) ?>"><span class="icon back"><?php echo $this->translator->translator('common_back'); ?></span></a></span>
        <?php } ?>
        <span class="button ui-widget-content-1"><a href="javascript:  void(0);" class="reset_btn"><span class="icon reset"><?php echo $this->translator->translator('common_reset'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript: void(0);" class="add_article_btn"><span class="icon save"><?php echo $this->translator->translator('common_save'); ?></span></a></span>
    </div>
</div>
<?php endif; ?>
</div>

<script type="text/javascript">
	openCodeMirror('article_content');
</script>